//
//  AppealViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/8.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppealViewModel: BaseViewModel {
    
    var photoArr = [UIImage]()
    
    var tradeId: Int = 0
    var res: String = ""
    var des: String = ""
    var picPath: String = ""
    
    private lazy var appealApi: AppealRequest = {
        let api = AppealRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    ///上传图片，获取图片地址
    private lazy var uploadApi: UpLoadFileRequest = {
        let portrait = UpLoadFileRequest()
        portrait.dataReformer = self
        portrait.paramSource = self
        portrait.delegate = self
        return portrait
    }()
    
    private func appeal(picPath: String) {
        self.picPath = picPath
        self.appealApi.loadData()
    }
    
    ///上传申诉图片
    func uploadAppelImage(res: String,des: String,images: [UIImage]) {
        self.photoArr = images
        self.res = res
        self.des = des
        if images.count == 0 {
            self.appealApi.loadData()
        }else {
            self.uploadApi.loadData()
        }
    }
    
}

extension AppealViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? AppealRequest {
            CYMBShow.showMessage(kText("申诉成功"))
            self.controller?.navigationController?.popViewController(animated: true)
        }else if let _ = request as? UpLoadFileRequest {
            if let files = data?["data"] as? [[String:Any]]{
                var picPath = ""
                for file in files {
                    if let imagePath = file["relativePath"] as? String {
                        picPath = picPath + imagePath + ","
                    }
                }
                picPath.removeLast()
                self.appeal(picPath: picPath)
            }else {
                CYMBShow.showMessage(kText("申诉失败"))
            }
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? AppealRequest {
            CYMBShow.showMessage(request.errorMsg)
        }else if let _ = request as? UpLoadFileRequest {
            CYMBShow.showMessage(request.errorMsg)        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? AppealRequest {
            pramas.updateValue(self.tradeId, forKey: "tradeId")
            pramas.updateValue(self.res, forKey: "reason")
            pramas.updateValue(self.des, forKey: "description")
            pramas.updateValue(picPath, forKey: "picPath")
        }else if let _ = request as? UpLoadFileRequest {
            pramas.updateValue("orderAppeal", forKey: "type")
            let files = self.photoArr.map { (image) -> File in
              let img = image.compressToSize(CGSize(width: 160, height: 160))
              let imageData = UIImageJPEGRepresentation(img, 1.0)!
              return File(fileData: imageData, fileName: String(format: "%p.jpeg", image), fileMIMEType: "image/jpeg")
            }
            pramas.updateValue(files, forKey: "files")
        }
        return pramas
    }
    
}
